package com.liang.service.user;

import com.liang.dao.BaseDao;
import com.liang.dao.user.UserDao;
import com.liang.dao.user.UserDaoImpl;
import com.liang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{
    //业务层都会调用dao层，所以我们要引入dao层！
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) throws Exception {
        Connection connection = null;
        User user = null;

        connection = BaseDao.getConnection();
        user  = userDao.getLoginUser(connection, userCode);
        BaseDao.close(connection,null,null);

        return user;

    }

    @Override
    public boolean updatePwd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if(userDao.updatePwd(connection, id, pwd)>=1){
                flag = true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.close(connection,null,null);
        }

        return flag;
    }

    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.close(connection,null,null);
        }
        return count;
    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        System.out.println("queryUserName ---- > " + queryUserName);
        System.out.println("queryUserRole ---- > " + queryUserRole);
        System.out.println("currentPageNo ---- > " + currentPageNo);
        System.out.println("pageSize ---- > " + pageSize);
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.close(connection, null, null);
        }
        return userList;
    }

    @Override
    public boolean addUser(User user) {
        Connection connection = null;
        int updateRows = 0;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            //增删改操作需要开启jdbc事务管理
            connection.setAutoCommit(false);
            updateRows = userDao.addUser(connection, user);
            connection.commit();
            if(updateRows>0){
                flag = true;
            }else{
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }finally {
            BaseDao.close(connection,null,null);
        }
        return flag;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        int count = userService.getUserCount(null, 0);
        System.out.println(count);
    }
}
